﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ProcessingLite
{
    /// <summary>
    /// Interaction logic for ProcessingLiteMainWindow.xaml
    /// </summary>
    public partial class ProcessingLiteMainWindow : Window, IDrawer
    {
        public ProcessingLiteMainWindow()
        {
            InitializeComponent();
        }

        #region Methods\\DrawingCommands
        
        public void FillPixel(int x, int y, Color color)
        {
            FillRectangle(x, y, 1, 1, color);
        }

        public void FillRectangle(int x, int y, int height, int width, Color color)
        {
            Dispatcher.Invoke(new Action(
                () =>
                {
                    Rectangle rec = new Rectangle();
                    Canvas.SetTop(rec, y);
                    Canvas.SetLeft(rec, x);
                    rec.Width = width;
                    rec.Height = height;
                    rec.Opacity = 0.0;
                    rec.Fill = new SolidColorBrush(color);
                    drawingCanvas.Children.Add(rec);

                    DoubleAnimation opacityAnimation = new DoubleAnimation();
                    opacityAnimation.From = 0.0;
                    opacityAnimation.To = 1.0;
                    opacityAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(1));

                    Storyboard board = new Storyboard();
                    board.Children.Add(opacityAnimation);
                    Storyboard.SetTarget(opacityAnimation, rec);
                    Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity"));

                    board.Completed += (obj, args) =>
                    {
                        this.OnDrawingCompleted();
                    };

                    board.Begin();
                }
            ));
        }

        #endregion

        public event EventHandler DrawingCompleted;
        private void OnDrawingCompleted()
        {
            EventHandler handler = DrawingCompleted;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }

        
        
    }
}
